package com.kuang.servlet;

import jdk.nashorn.internal.objects.NativeError;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.PublicKey;
import java.util.List;
import java.util.UUID;

public class FileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //判断上传的表单是普通表单还是带文件的表单
        if (!ServletFileUpload.isMultipartContent(request)){
            return;//说明这是一个普通的表单，停止运行
        }


        //创建上传文件的保存目录，建议在web-inf下，防止用户直接访问到资源
        String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
        File uploadFile = new File(uploadPath);
        if (!uploadFile.exists()){
            uploadFile.mkdir();//文件不存在，创建这个目录
        }


        //创建上传文件的临时目录，建议在web-inf下，防止用户直接访问到资源
        String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
        File tempFile = new File(tempPath);
        if (!tempFile.exists()){
            tempFile.mkdir();//文件不存在，创建这个目录
        }

        //1.创建diskfileitemfactory对象，处理文件或者限制上传大小
        DiskFileItemFactory factory = new DiskFileItemFactory();

        //2.获取servletfileupload
        ServletFileUpload upload = new ServletFileUpload(factory);
        //处理上传的文件





    }



    //来写三个方法，以供上面的方法完成调用
    public static DiskFileItemFactory getDiskFileItemFactory(File file){
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(1024*1024);//缓冲区
        factory.setRepository(file);//临时目录
        return factory;
    }

    public static ServletFileUpload getServletFileUpload(DiskFileItemFactory factory){

    }

}
